{% extends 'layout.html' %}
{% load color %}
{% load permission %}
{% load static %}

{% block content %}
    <div class="table">
        <div class="add_btn" id="addBtn">
            <button class="btn btn-success">
                <i class="fas fa-plus-circle"></i>
                <span> 添加</span>
            </button>
        </div>

        <table class="table table-bordered">
            <thead>
            <tr>
                <th>ID</th>
                <th>类型</th>
                <th>金额</th>
                <th>订单号</th>
                <th>时间</th>
                <th>备注</th>
            </tr>
            </thead>
            <tbody>
            {% for item in pager.queryset %}
                <tr row-id="{{ item.id }}">
                    <th scope="row">{{ item.id }}</th>
                    <td>
                        <button class="btn btn-xs btn-{{ item.charge_type|color }}">{{ item.get_charge_type_display }}</button>
                    </td>
                    <td>{{ item.amount }}</td>
                    <td>
                        {% if item.order_oid %}
                            {{ item.order_oid }}
                        {% else %}
                            -
                        {% endif %}
                    </td>
                    <td>{{ item.create_time|date:'Y-m-d' }}</td>
                    <td>
                        {% if item.memo %}
                            {{ item.memo }}
                        {% else %}
                            -
                        {% endif %}
                    </td>
                </tr>
            {% endfor %}
            </tbody>
        </table>


        <nav aria-label="Page navigation">
            <ul class="pagination">
                {{ pager.html }}
            </ul>
        </nav>

        <!-- Modal -->
        <div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                                aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title" id="myModalLabel">添加交易记录</h4>
                    </div>

                    <div class="modal-body">
                        <form class="form-horizontal" id="addForm">
                            {% csrf_token %}

                            {% for field in form %}
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">{{ field.label }}</label>
                                    <div class="col-sm-9">
                                        {{ field }}
                                        <span class="data_error">{{ field.errors.0 }}</span>
                                    </div>
                                </div>
                            {% endfor %}
                        </form>
                    </div>

                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                        <button type="button" class="btn btn-primary" id="addBtnSubmit">保 存</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
{% endblock %}

{% block js %}
    <script>
        $(function () {
            $('#addBtn').click(function () {
                $('#addModal').modal('show')
            })

            $('#addBtnSubmit').click(function () {
                $('.data_error').empty();
                $.ajax({
                    url: '{% url "customer_charge_add" pk=pk %}',
                    type: 'POST',
                    data: $('#addForm').serialize(),
                    dataType: 'JSON',
                    success: function (res) {
                        if (res.status) {
                            window.location.reload()
                            $('#addModal').modal('hide')
                        } else {
                            $.each(res.detail, function (k, v) {
                                $('#id_' + k).next().text(v[0]);
                            })
                        }
                    }
                })
            })
        })
    </script>
{% endblock %}
